package com.bienvenue.main.ui.activity.personal;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.bienvenue.main.R;
import com.bienvenue.main.base.BaseActivity;
import com.bienvenue.main.httpService.ResponseObserver;
import com.bienvenue.main.model.HttpBean;
import com.bienvenue.main.serviceApi.ServiceModel;
import com.bienvenue.main.ui.activity.enterprise.EnterpriseRegisterActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: Bienvenue
 * @Package: com.bienvenue.main.ui.activity
 * @ClassName: PersonalRegisterActivity
 * @Description: java类作用描述
 * @Author: Mr.Chen
 * @CreateDate: 2020/9/9 13:49
 * @UpdateUser: Mr.Chen
 * @UpdateDate: 2020/9/9 13:49
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */


public class PersonalRegisterActivity extends BaseActivity {
    @BindView(R.id.et_register_phone)
    EditText etRegisterPhone;
    @BindView(R.id.et_register_pwd)
    EditText etRegisterPwd;
    @BindView(R.id.et_register_enter_pwd)
    EditText etRegisterEnterPwd;
    @BindView(R.id.et_login_sms_code)
    EditText etLoginSmsCode;
    @BindView(R.id.et_register_user_name)
    EditText etRegisterUserName;
    @BindView(R.id.tv_send_sms_code)
    TextView tvSendSmsCode;

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal_register;
    }

    @OnClick(R.id.iv_return_btn)
    void onFinish() {
        this.finish();
    }

    @OnClick(R.id.tv_send_sms_code)
    void onSendSmsCode() {
        if (!TextUtils.isEmpty(etRegisterPhone.getText().toString().trim()) && etRegisterPhone.getText().toString().length() == 11) {
            smsCode();
        } else {
            showToast("请输入正确的手机号");
        }
    }
    public void smsCode(){
        CountDownTimer mTimer=new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvSendSmsCode.setTextColor(Color.GRAY);
                tvSendSmsCode.setText(millisUntilFinished/1000+"秒后重发");
            }

            @Override
            public void onFinish() {
                tvSendSmsCode.setEnabled(true);
                tvSendSmsCode.setTextColor(getColor(R.color.color_478CFE));
                tvSendSmsCode.setText("获取验证码");
                cancel();
            }
        };
        mTimer.start();
        ServiceModel.getServiceModel().onSuperCode(etRegisterPhone.getText().toString().trim(), new ResponseObserver<HttpBean>() {
            @Override
            public void onSuccess(HttpBean response) {
                if (response.getCode()==200){
                    showToast("验证码已下发，请注意查收");
                    tvSendSmsCode.setEnabled(false);
                }else{
                    showToast(response.getMsg());
                    tvSendSmsCode.setEnabled(false);
                }
            }

            @Override
            public void onFail(String message) {
                showToast(message);
                tvSendSmsCode.setEnabled(false);
            }
        });
    }
    @OnClick(R.id.tv_register_btn)
    void onRegister() {

        if (TextUtils.isEmpty(etRegisterUserName.getText().toString().trim())) {
            showToast("请输入用户名");
        } else if (TextUtils.isEmpty(etRegisterPhone.getText().toString().trim()) || etRegisterPhone.getText().toString().trim().length() <= 10) {
            showToast("请输入正确的手机号");
        } else if (TextUtils.isEmpty(etRegisterPwd.getText().toString().trim()) || etRegisterPwd.getText().toString().trim().length() <= 5) {
            showToast("请输入正确的密码");
        } else if (TextUtils.isEmpty(etRegisterEnterPwd.getText().toString().trim()) || etRegisterEnterPwd.getText().toString().trim().length() <= 5 || !etRegisterPwd.getText().toString().trim().equals(etRegisterEnterPwd.getText().toString().trim())) {
            showToast("两次密码输入不一致");
        } else if (TextUtils.isEmpty(etLoginSmsCode.getText().toString().trim())) {
            showToast("请输入验证码");
        } else {
            ServiceModel.getServiceModel().onRegister(etRegisterUserName.getText().toString().trim(), etRegisterEnterPwd.getText().toString().trim(), etRegisterPhone.getText().toString().trim(), new ResponseObserver<HttpBean>() {
                @Override
                public void onSuccess(HttpBean response) {
                    if (response.getCode()==200){
                        startActivity(new Intent(PersonalRegisterActivity.this,PhoneSmsLoginActivity.class));
                    }else{
                        showToast(response.getMsg());
                    }
                }

                @Override
                public void onFail(String message) {
                    showToast(message);
                }
            });
        }
    }

    @OnClick(R.id.tv_enterprise_register)
    void onEnterpriseRegister() {
        startActivity(new Intent(PersonalRegisterActivity.this, EnterpriseRegisterActivity.class));
        this.finish();
    }
}
